/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.gar.bc;

import edu.upn.sigemfi.gar.be.Agricola;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceUnit;

/**
 *
 * @author Programador
 */
@Stateless
public class AgricolaFacade implements AgricolaFacadeLocal {
    //@PersistenceUnit(name="edu.upn.sigemfi.gar-ejbPU")
    @PersistenceContext
    private EntityManager em;

    public void create(Agricola agricola) {
        em.persist(agricola);
    }

    public void edit(Agricola agricola) {
        em.merge(agricola);
    }

    public void remove(Agricola agricola) {
        em.remove(em.merge(agricola));
    }

    public Agricola find(Object id) {
        return em.find(Agricola.class, id);
    }

    public List<Agricola> findAll() {
        return em.createQuery("select object(o) from Agricola as o").getResultList();
    }

    public Agricola findByIdGar(Long id) {
        return (Agricola) em.createQuery("select object(o) from Agricola as o inner join o.garantiaPrendaria Garantia where Garantia.idGarantia ="+id.toString()).getSingleResult();
    }
}
